<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
	<link href="base/css/selector.css" rel="stylesheet" type="text/css"></link>
	<style type="text/css">
		li {list-style: none;}
		li label {width: 200px;}
		.tree-comment {border:solid 1px #aaa;padding:10px;}
		.comment-button {border: none; width: 100px; height: 40px; margin-top: 10px; background: #27bcf2; color: #FFF; cursor: pointer;}
		.comment-button:hover {background: #053b75;}
		.tree-comment-hide {display: none;}
		textarea {vertical-align: top;}
	</style>
  </head>
  <body>
    <form id="form" method="post">
        <input name="id" class="mini-hidden" />
        <fieldset style="border:solid 1px #aaa;padding:10px;">
            <legend >书籍信息</legend>
            <div style="padding:5px;">
            <table class="editor_table">
            	<tr>
                    <td style="width:70px;">类型：</td>
                    <td style="width:150px;">
                      <!-- <input name="typeId" id="typeId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                        url="listType.do?isNotPage=1" value=""  allowInput="false" showNullItem="true" nullItemText="请选择..." onvaluechanged="onValueChanged" />
                        -->
                      <div class="my-selector s_type_selector" textField="name" valueField="id" dataField="code" url="listType.do?isNotPage=1"
                  		onValueChanged="onTypeChanged" className="type-selector" debug=0></div> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">类别：</td>
                    <td style="width:150px;">
                    	<!-- <input name="categoryId" id="categoryId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                        url="listCategory.do?isNotPage=1" allowInput="false" showNullItem="true" nullItemText="请选择..."/>  -->
                        <div class="my-selector s_category_selector" textField="name" valueField="id" dataField="code" url=""
                  			onValueChanged="" className="category-selector" debug=0></div>
                    </td>
                </tr>
            	<tr>
                    <td style="width:200px;">书名：</td>
                    <td style="width:150px;">
                    	<input name="name" id="name" class="mini-textbox" required="true" vtype="maxLength:50" emptyText="请输入书籍"/>
                    </td>
                </tr>
                <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="img" id="img" class="mini-textbox" required="true" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:200px;">描述：</td>
                    <td style="width:150px;">
                    	<textarea name="description" id="description" class="mini-textarea editor_len" required="true" vtype="maxLength:150" emptyText="请输入描述"></textarea>
                    </td>
                </tr>
                <tr>
                    <td style="width:200px;">目录：</td>
                    <td style="width:150px;">
                    	<textarea name="directory" id="directory" class="mini-textarea editor_len" required="true" vtype="maxLength:250" emptyText="请输入目录"></textarea>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">选择配图：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload" fileCountLimit="1" multi="true" type="file" 
                      	showImage="true" onuploadsuccess="onUploadSuccess"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">ISBN号：</td>
                    <td style="width:150px;">
                     <input name="isbn" id="isbn" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入ISBN号"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">库存(数字)：</td>
                    <td style="width:150px;">
                     <input name="reportory" id="reportory" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入库存"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">作者：</td>
                    <td style="width:150px;">
                     <input name="author" id="author" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">出版社：</td>
                    <td style="width:150px;">
                     <input name="publisher" id="publisher" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">出版时间：</td>
                    <td style="width:150px;">
                     <input name="timePublish" id="timePublish" class="mini-datepicker" format="yyyy-MM-dd" value="2014-01-01"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">评级(数字1-5)：</td>
                    <td style="width:150px;">
                     <input name="rate" id="rate" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">售价(数字)：</td>
                    <td style="width:150px;">
                     <input name="priceSelling" id="priceSelling" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">原价(数字)：</td>
                    <td style="width:150px;">
                     <input name="priceOriginal" id="priceOriginal" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">版次(数字)：</td>
                    <td style="width:150px;">
                     <input name="version" id="priceOriginal" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">印刷时间：</td>
                    <td style="width:150px;">
                     <input name="printDate" id="printDate" class="mini-datepicker" format="yyyy-MM-dd" value="2014-01-01"/> 
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">印次（数字）：</td>
                    <td style="width:150px;">
                     <input name="printTime" id="printTime" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">页数（数字）：</td>
                    <td style="width:150px;">
                     <input name="pagecount" id="pagecount" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr style="display: none;">
                    <td style="width:70px;"></td>
                    <td style="width:150px;">
                     <input name="ctCode" id="ctCode" class="mini-textbox editor_len" vtype="maxLength:1000" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">字数（数字）：</td>
                    <td style="width:150px;">
                     <input name="wordcount" id="wordcount" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">开本：</td>
                    <td style="width:150px;">
                     <input name="booksize" id="booksize" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">包装类型：</td>
                    <td style="width:150px;">
                     <input name="pack" id="pack" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">纸张类型：</td>
                    <td style="width:150px;">
                     <input name="paper" id="paper" class="mini-textbox editor_len" vtype="maxLength:50" emptyText="请输入"/>  
                    </td>
                </tr>
            </table>   
            
            <div id="floors-wrapper">
              <ul class="tree-comment-hide">
                <li><label>学校：</label>
                  <div class="my-selector" textField="name" valueField="id" dataField="code" url="listSchool.do?isNotPage=1"
                  onValueChanged="onSchoolChanged" className="school-selector" debug=0>
                    
                  </div>
                    </li>
                <li>
                    <label>院系：</label>
                  <div class="my-selector s_department-selector" textField="name" valueField="id" dataField="code" url=""
                  onValueChanged="onDepartmentChanged" className="department-selector" debug=0>
                    
                  </div>
                </li>
                  <li>
                    <label>专业：</label>
                    <div class="my-selector s_major-selector" textField="name" valueField="id" dataField="code" url=""
                      onValueChanged="" className="major-selector" debug=0>
                    
                    </div>
                  </li>
                  <li>
                    <label>学年：</label>
                    <div class="my-selector s_year-selector" textField="name" valueField="id" dataField="code" url="year.do"
                      onValueChanged="" className="year-selector" debug=0>
                    
                    </div>
                  </li>
                <li><input type="button" class="del-floor-button" value="删除此类型" /></li>
              </ul>
              
              <input type="button" class="comment-button" value="添加新学校分类" />
            </div>
         </div>
        </fieldset>
        
        <div style="text-align:center;padding:10px;">
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>
	<script src="${path}/base/js/my-selector.js" type="text/javascript"></script>
    <script type="text/javascript">
    	var id = '';
        mini.parse();
        var grid = mini.get("datagrid1");
        var form = new mini.Form("form");
        load($('.s_type_selector'));
        load($('.s_category_selector'));
        function SaveData() {
			var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            var json = mini.encode(o);
            $.ajax({
                url: "saveBook.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                	if(text.indexOf('true') > -1){
				    	mini.alert("操作成功");
				    	CloseWindow("save");
				    }else{
				    	mini.alert("操作失败");
				    	CloseWindow();
				    }
                },
                error: function (textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
        
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                $.ajax({
                    url: "findByIdBook.do?id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        id = o.id;
                        var imgs = mini.get("img").getValue();
                        if(imgs != ''){
               			 $('#file_upload').setFileId(imgs,true,true);
               		    }
               		    
               		    //设置类型信息状态
               		    var cts = o.ctCodeList;
               		    for(var i = 0, len = cts.length; i < len; i++) {
               		    	var ct = cts[i];
               		    	var schoolId = ct.schoolId;
		 					var schoolCode = ct.schoolCode;
				 			var schoolName = ct.schoolName;
		 		 			var departmentId = ct.departmentId;
    			 			var departmentCode = ct.departmentCode;
    	 		 			var departmentName = ct.departmentName;
    			 			var majorId = ct.majorId;
    	 		 			var majorCode = ct.majorCode;
    			 			var majorName = ct.majorName;
    	 		 			var typeId = ct.typeId;
    	 		 			var typeCode = ct.typeCode;
    			 			var typeName = ct.typeName;
    	 		 			var categoryId = ct.categoryId;
    	 		 			var categoryCode = ct.categoryCode;
    			 			var categoryName = ct.categoryName;
    			 			var yearName = ct.yearName;
    			 			var yearCode = ct.yearCode;
    			 			var yearId = ct.yearId;
    			 			
    			 			mySetValue($('.type-selector'), {name : typeName, value : typeId, data : typeCode});
    			 			onTypeChanged($('.type-selector').parent());
    			 			mySetValue($('.category-selector'), {name : categoryName, value : categoryId, data : categoryCode});
    			 			
    			 			if(schoolCode != 'S00') {
    			 				createNewCT($('.comment-button'), {s : {name : schoolName, value : schoolId, data : schoolCode},
    			 				 d : {name : departmentName, value : departmentId, data : departmentCode}, 
    			 				 m : {name : majorName, value : majorId, data : majorCode},
    			 				 y : {name : yearName, value : yearId, data : yearCode}});
    			 			}
               		    }
               		    
                        grid.load({id : id});
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "cancel" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                	SaveData();
                	return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        }
        function onOk(e) {
        	getCtCode();
        	var imgId = mini.get("img").getValue();
        	if(imgId == ""){
        		mini.alert("请上传图片");
        		return ;
        	}
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        
        function onValueChanged() {
        	var typeCB = mini.get("typeId");
            var categoryCB = mini.get("categoryId");
            var typeId = typeCB.getValue();
            categoryCB.load("listCategory.do?isNotPage=1&pid=" + typeId);
        }
        
        $('.comment-button').click(function() {
        	createNewCT($(this));
        });
        
        function createNewCT(site, initData) {
        	var floor = $('.tree-comment-hide').eq(0).clone();
        	floor.attr("class", "tree-comment");
        	
        	createAllSelector(floor);
        	
        	var floors = $('.tree-comment');
        	var floorCount = 1;
        	if(floors.length > 0) {
        		var lastFloor = floors.eq(floors.length - 1);
        		floorCount = parseInt(lastFloor.find('.floorCount').text()) + 1;
        	}
        	floor.find('.floorCount').text(floorCount);
        	site.before(floor);
        	
        	$('.del-floor-button').click(function() {
        		var parentUl = $(this).parent().parent();
        		parentUl.remove();
        	});
        	
        	if(initData != null) {
        		mySetValue(floor.find('.school-selector'), initData.s);
        		onSchoolChanged(floor.find('.school-selector').parent());
        		mySetValue(floor.find('.department-selector'), initData.d);
        		onDepartmentChanged(floor.find('.department-selector').parent());
        		mySetValue(floor.find('.major-selector'), initData.m);
        		mySetValue(floor.find('.year-selector'), initData.y);
        	}
        }
        
        function onTypeChanged(wrapper) {
        	var pid = wrapper.find('.s_li_show').attr('idv');
    		var s = $('.s_category_selector');
    		reload(s, "listCategory.do?isNotPage=1&pid=" + pid);
        }
        
        function onSchoolChanged(wrapper) {
    		var pid = wrapper.find('.s_li_show').attr('idv');
    		var s = wrapper.parent().parent('.tree-comment').find('.s_department-selector');
    		reload(s, "listDepartment.do?isNotPage=1&pid=" + pid);
  		}

  		function onDepartmentChanged(wrapper) {
    		var pid = wrapper.find('.s_li_show').attr('idv');
    		var s = wrapper.parent().parent('.tree-comment').find('.s_major-selector');
    		reload(s, "listMajor.do?isNotPage=1&pid=" + pid);
  		}
  		
  		function getCtCode() {
  			var resCode = '';
  		
  			var cts = $('.tree-comment');
  			
  			var tCode = $('.type-selector .s_li_show').attr('data');
  			var cCode = $('.category-selector .s_li_show').attr('data');

  			if(isEmpty(tCode) || isEmpty(cCode)) {
  				mini.alert('请填写类型和类别');
  				return;
  			}
  			var temp = '';
  			
  			var len = cts.length;
  			if(len > 0) {
  				for(var i = 0; i < len; i++) {
  				var ct = cts.eq(i);
  				var sCode = ct.find('.school-selector .s_li_show').attr('data');
  				if(isEmpty(sCode))
  					sCode = 'S00';
  				var dCode = ct.find('.department-selector .s_li_show').attr('data');
  				if(isEmpty(dCode))
  					dCode = 'D00';
  				var mCode = ct.find('.major-selector .s_li_show').attr('data');
  				if(isEmpty(mCode))
  					mCode = 'M00';
  				var yCode = ct.find('.year-selector .s_li_show').attr('data');
  				
  				if(isEmpty(yCode)) {
  					yCode = 'Y00';
  				}
  				resCode += temp + sCode + dCode + mCode + tCode + cCode + yCode;
  				temp = ',';
  				}
  			} else {
  				resCode = 'S00D00M00' + tCode + cCode;
  			}
  			  			mini.alert(resCode);
  			mini.get("ctCode").setValue(resCode);
  		}
  		
  		
    </script>
    
  </body>
</html>
